<template>
  <div>
    <!-- 列表页面 -->
    <div class="container" v-if="!showEdit">
      <div class="header">
        <div class="title">楼栋列表</div>
<!--        <el-button style="margin-left:30px;"-->
<!--                   @click.prevent="addBuilding"-->
<!--                   plain-->
<!--                   type="primary"-->
<!--                   size="medium">添加楼栋</el-button-->
<!--        >-->
      </div>
      <!-- 表格 -->
      <lin-table
        :tableColumn="tableColumn"
        :tableData="tableData"
        :operate="operate"
        @handleEdit="handleEdit"
        @handleDelete="handleDelete"
        @row-click="rowClick"
        v-loading="loading"
      ></lin-table>
    </div>
    <!-- 编辑页面 -->
    <building-modify v-else-if="handleEdit" @editClose="editClose" :editBuildingID="editBuildingID"></building-modify>
    <!--添加页面-->
<!--    <building-create v-if="showCreate" :isCreate="showCreate"></building-create>-->
  </div>
</template>

<script>
import building from '@/model/ag_building'
import LinTable from '@/component/base/table/lin-table'
import BuildingModify from '@/view/ag_basicinfo/ag_building/building-edit'
export default {
  components: {
    BuildingModify,
    LinTable,
  },
  data() {
    return {
      showCreate: false,
      tableColumn: [
        // {prop:'id',label: '楼栋编号'},
        { prop: 'name', label: '楼栋名称' },
        { prop: 'local', label: '楼栋位置' },
        { prop: 'coordinates', label: '楼栋坐标' },
        { prop: 'manager', label: '楼栋联系人' },
        { prop: 'phone', label: '联系方式' },
      ],
      tableData: [],
      operate: [],
      showEdit: false,
      editBuildingID: 1,
    }
  },
  async created() {
    this.loading = true
    await this.getBuildings()

    this.operate = [
      { name: '编辑', func: 'handleEdit', type: 'primary' },
      {
        name: '删除',
        func: 'handleDelete',
        type: 'danger',
        permission: '删除楼栋',
      },
    ]
    this.loading = false
  },
  methods: {
    async getBuildings() {
      try {
        const buildings = await building.getBuildings()
        this.tableData = buildings
      } catch (error) {
        if (error.code === 10020) {
          this.tableData = []
        }
      }
    },



    handleEdit(val) {
      // console.log('val', val)
      this.showEdit = true
      this.editBuildingID = val.row.id
    },
    handleDelete(val) {
      this.$confirm('此操作将永久删除该楼栋, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const res = await building.deleteBuilding(val.row.id)
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.getBuildings()
          this.$message({
            type: 'success',
            message: `${res.message}`,
          })
        }
      })
    },
    rowClick() {},
    editClose() {
      this.showEdit = false
      this.getBuildings()
    },
    addBuilding(){
      console.log('添加楼栋')
    }
  },
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;

  .header {
    display: flex;
    justify-content: flex-start;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      margin-right: 30px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }

    .header-left {
      float: right;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
}
</style>
